package org.daverog.jaxrs.iodocs;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.FileWriter;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Anjana Kulasinghe
 * Date: 9/26/13
 */


public class ConvertObjects {

    private List<String> objectList = new ArrayList<String>();
    String tempFileLocation = "/home/ak40260/MyProjects/jaxrs-iodocs/src/main/java/org/daverog/jaxrs/iodocs/temp/";
    String tempFilePackage = "org.daverog.jaxrs.iodocs.temp";


    public String convertJavaObjectToJasonString(String className) {

        String baseClassName = null;
        try {
            generateClassByName(Class.forName(className).getName());
            baseClassName = Class.forName(className).getSimpleName();
        } catch (Exception ignored) {
        }

        try {
            Field[] newFieldList = Class.forName(className).getDeclaredFields();
            do {
                className = javaObjectProcessor(newFieldList);
                newFieldList = Class.forName(className).getDeclaredFields();
            }
            while (newFieldList != null);
        } catch (Exception ignored) {
        }

        generateClass();
        return convertJavaToJason(tempFilePackage + "." + baseClassName);
    }


    public String javaObjectProcessor(Field[] newFieldList) {

        for (Field field : newFieldList) {
            try {
                field.getType().newInstance();
                objectList.add(field.getType().newInstance().getClass().getName());
                return field.getType().newInstance().getClass().getName();
            } catch (Exception ignored) {
            }
        }
        return null;
    }

    public void generateClass() {
        for (String anObjectList : objectList) {
            generateClassByName(anObjectList);
        }
    }

    public void generateClassByName(String className) {
        Field[] newFieldList = null;
        List<String> itemList = new ArrayList<String>();

        itemList.add("package " + tempFilePackage + ";");
        try {
            itemList.add("public class " + Class.forName(className).getSimpleName() + " {");
            newFieldList = Class.forName(className).getDeclaredFields();
        } catch (Exception ignored) {
        }

        for (Field field : newFieldList) {
            try {
                if (field.getType().getName().equals("java.lang.String")) {
                    itemList.add("private " + field.getType().getName() + " " + field.getName() + ";");
                } else {
                    field.getType().newInstance().getClass();
                    itemList.add("private " + tempFilePackage + "." + field.getType().getSimpleName() + " " + field.getType().getSimpleName() + " = new " + tempFilePackage + "." + field.getType().getSimpleName() + "();");
                }
            } catch (Exception ignored) {
                itemList.add("private " + field.getType().getName() + " " + field.getName() + ";");
            }
        }
        itemList.add("}");

        try {
            FileWriter writer = new FileWriter(tempFileLocation + Class.forName(className).getSimpleName() + ".java");
            for (String str : itemList) {
                writer.write(str);
                writer.write("\n");
            }
            writer.close();
        } catch (Exception ignored) {
        }
    }

    // With Gson
    public String convertJavaToJason(String className) {
        Gson gson = new GsonBuilder()
                .enableComplexMapKeySerialization()
                .serializeNulls()
                .setDateFormat(DateFormat.LONG)
                .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
                .setPrettyPrinting()
                .setVersion(1.0)
                .create();

        try {
            Object object = Class.forName(className).newInstance();
            return gson.toJson(object);
        } catch (Exception e) {
            return e.getMessage();
        }
    }


    ////////////////////


    public String convertJavaObjectToJasonString111(String className) {

        String baseClassName = null;
        try {
            generateClassByName(Class.forName(className).getName());
            baseClassName = Class.forName(className).getSimpleName();
        } catch (Exception ignored) {
        }

        try {
            Field[] newFieldList = Class.forName(className).getDeclaredFields();
            do {
                className = javaObjectProcessor(newFieldList);
                newFieldList = Class.forName(className).getDeclaredFields();
            }
            while (newFieldList != null);
        } catch (Exception ignored) {
        }

        generateClass();
        return convertJavaToJason(tempFilePackage + "." + baseClassName);
    }


    public String javaObjectProcessor111(Class myClass) {
        Field[] newFieldList = null;
        List<String> itemList = new ArrayList<String>();
        try {

            newFieldList = myClass.getDeclaredFields();
        } catch (Exception ignored) {
        }

        for (Field field : newFieldList) {
            try {
                Class newClass = Class.forName(field.getType().getName());
                javaObjectProcessor111(newClass);
            } catch (Exception ignored) {
            }
        }
        return null;
    }


    public void generateClassByName1111(String className) {

        try {

            Class myClass = Class.forName(className);

        } catch (Exception ignored) {
        }
    }
}
